package com.wangzhixuan.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wangzhixuan.commons.utils.FirstWord;
import com.wangzhixuan.commons.utils.HttpClientUtil;
import com.wangzhixuan.commons.utils.PageInfo;
import com.wangzhixuan.commons.utils.excel.ReadExcel;
import com.wangzhixuan.model.CusGroup;
import com.wangzhixuan.model.Customer;
import com.wangzhixuan.model.EleRate;
import com.wangzhixuan.model.EleSpec;
import com.wangzhixuan.model.Metering;
import com.wangzhixuan.model.vo.ExcelCustomer;
import com.wangzhixuan.model.vo.NewCustomerVo;
import com.wangzhixuan.mapper.CusGroupMapper;
import com.wangzhixuan.mapper.CustomerMapper;
import com.wangzhixuan.mapper.EleRateMapper;
import com.wangzhixuan.mapper.EleSpecMapper;
import com.wangzhixuan.mapper.MeteringMapper;
import com.wangzhixuan.service.ICustomerService;
import com.baomidou.mybatisplus.plugins.Page;

import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-08-05
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
	
	@Autowired
    private CustomerMapper customerMapper;
    
    @Autowired
    private EleRateMapper eleRateMapper;
    
    @Autowired
    private EleSpecMapper eleSpecMapper;
    
    @Autowired
    private MeteringMapper meteringMapper;
    
    @Autowired
    private CusGroupMapper cusGroupMapper;
    
	@Override
    public void selectDataGrid(PageInfo pageInfo) {
    	Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
        page.setOrderByField(pageInfo.getSort());
        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<NewCustomerVo> list = customerMapper.selectCustomerPage(page, pageInfo.getCondition());
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }

	@Override
	public List<Map<String, Object>> selectList(Integer userId) {
		return customerMapper.selectList(userId);
	}

	@Override
	public Map<String, Object> oneCusDeatile(Integer oneCusId) {
		Map<String,Object> oneCusDeatile = customerMapper.oneCusDeatile(oneCusId);
		return oneCusDeatile;
	}

	@Override
	public Boolean uploadExcel(String name, MultipartFile excel,Integer id,String rootPath) {
		boolean b = false;
        //创建处理EXCEL
        ReadExcel readExcel=new ReadExcel();
        //解析excel，获取客户信息集合。
        List<ExcelCustomer> excelInfo = readExcel.getExcelInfo(name ,excel,rootPath);
        Date date = new Date();
        for(ExcelCustomer cus : excelInfo){
        	
        	String eleRate = cus.getEleRate();						 //获得倍率
        	Integer rateId = eleRateMapper.selectRateId(eleRate);
        	if(rateId == null){  								//如果倍率不存在,则添加
        		EleRate rate = new EleRate();
        		rate.setEleRate(eleRate);
        		eleRateMapper.insert(rate);
        		rateId = rate.getId();			//拿到插入的id
        	}
        	
        	String eleSpec = cus.getEleSpec(); 							//获得规格
        	Integer specId = eleSpecMapper.selectSpecId(eleSpec);
        	if(specId == null){  								//如果规格不存在,则添加
        		EleSpec spec = new EleSpec();
        		spec.setEleSpec(eleSpec);
        		eleSpecMapper.insert(spec);		
        		specId = spec.getId();					//拿到插入的id
        	}
        	
        	String meterName = cus.getMeterName(); //获得计量装置
        	Integer meterId = meteringMapper.selectMeterId(meterName);
        	if(meterId == null){  								//如果计量装置不存在,则添加
        		Metering meter = new Metering();
        		meter.setMeterName(meterName);
        		meteringMapper.insert(meter);
        		meterId = meter.getId();               //拿到插入的id
        	}
        	
        	String cusGroup = cus.getCusGroup(); //获得小区
        	Integer groupId = cusGroupMapper.selectCusGroupId(cusGroup);
        	if(groupId == null){  								//如果计量装置不存在,则添加
        		String spellscusGroup = FirstWord.getSpells(cusGroup);
        		String[] xy = HttpClientUtil.getXY(cusGroup);
        		CusGroup Group = new CusGroup();
        		Group.setxCode(xy[0]);
        		Group.setyCode(xy[1]);
        		Group.setCusGroup(cusGroup);
        		Group.setFirstWord(spellscusGroup);
        		cusGroupMapper.insert(Group);
        		groupId = Group.getId();               //拿到插入的id
        	}
        	String spells = FirstWord.getSpells(cus.getName());
        	Customer customer = new Customer();
        	customer.setName(cus.getName());
        	customer.setPhone(cus.getPhone());
        	customer.setCompanyAddress(cus.getCompanyAddress());
        	customer.setCreateId(id);
        	customer.setCreateTime(date);
        	customer.setAccountNumber(cus.getAccountNumber());
        	customer.setEleNum(cus.getEleNum());
        	customer.setRateId(rateId);
        	customer.setSpecId(specId);
        	customer.setMeterId(meterId);
        	customer.setGroupId(groupId);
        	customer.setInitials(spells);
        	customerMapper.insert(customer);
        }
		return null;
	}
}
